#!/bin/bash
gchar() {
	if [ ! -n "$1" ]; then
		return
	fi
	if [ ! -n "$2" ]; then
		set "$1" 0
	fi
	if [ $2 -ge 0 ]; then
		tmp="${1:$2}"
	else
		tmp="${1: $2}"
	fi
	echo "${tmp:0:1}"
}
cfgread() {
	line="$(echo "$1" | grep -Fxn "$2:" | cut -d ':' -f1 | sed -n '1p')"
	loop="$line"
	max="$(echo "$1" | sed -n '$=')"
	end=0
	# echo $line $loop $max
	while [ $loop -lt $max ]; do
		let loop=loop+1
		cont="$(echo "$1" | sed -n "${loop}p")"
		if [[ "$(gchar "$cont" -1)" == ':' && "$(gchar "$cont" -2)" != '\' ]]; then
			res="$(echo "$1" | sed -n "$[$line+1],$[$loop-1]p")"
			end=1
			break
		fi
	done
	if [ "$end" != "1" ]; then
		res="$(echo "$1" | sed -n "$[$line+1],${loop}p")"
	fi
	res="${res//'\:'/:}"
	res="${res//'\\'/\\}"
	echo "$res"
}
cfg() {
	if [ ! -n "$3" ]; then
		return
	fi
	if [ "$1" == "read" ]; then
		cfgread "$2" "$3"
	fi
}

tp="$1"
file="$2"
shift 2
for loop in "$@"
do
	cfg "$tp" "$(cat "$file")" "$loop"
done
